
#include "fttabwidget.h"
#include "ftpushbutton.h"
#include "ftpushbuttonicon.h"
#include "ftvboxlayout.h"
#include "fthboxlayout.h"

FTTabWidget::FTTabWidget(fttabwidget_mode m) :
    RotatingWidget()
{

    _mode = m;
    panels = new QStackedWidget();
    if ( _mode == full_page )
    {
        back_button = new FTPushButton( tr("Go to tab index...") );
        back_button->setIcon( QIcon(":/icons/level-up.png") );
        back_button->setIconSize( QSize(24,24) );
        back_button->hide();
        connect( back_button, SIGNAL(clicked()),
                 this, SLOT(indexTab()) );
        index_tab = new QWidget();
        panels->addWidget( index_tab );
        panels->setCurrentWidget( index_tab );
    }
    else
    {
        index_tab = NULL;
        back_button = NULL;
    }
}

FTTabWidget::~FTTabWidget()
{
    foreach( QAbstractButton* btn, buttons_group.buttons() )
        buttons_group.removeButton( btn );
}

void FTTabWidget::addTab( QWidget* tab, const QString& t)
{
    FTPushButton* button = new FTPushButton(t);
    appendTab( tab, button );
}

void FTTabWidget::addTab( QWidget* tab, const QIcon& i, FTControls::IconSize s)
{
    FTPushButtonIcon* button = new FTPushButtonIcon(i,s);
    appendTab( tab, button );
}

void FTTabWidget::appendTab( QWidget* w, QAbstractButton* b)
{
    b->setCheckable( true );
    buttons.append( b );
    buttons_group.addButton( b );
    bindings.insert( b,w );
    panels->addWidget( w );
    connect( b, SIGNAL(clicked()),
             this, SLOT(selectTab()) );
    if ( (_mode != full_page) && (bindings.count() == 1) )
        setCurrentWidget( w );
}

void FTTabWidget::addExtra(QWidget * w )
{
    extra_widgets.append( w );
}

void FTTabWidget::indexTab()
{
    if ( _mode == full_page )
    {
        panels->setCurrentWidget( index_tab );
        back_button->hide();
        emit currentChanged();
    }
}

void FTTabWidget::selectTab()
{
    QAbstractButton* btn = dynamic_cast<QAbstractButton *>(sender());
    if ( btn )
    {
        panels->setCurrentWidget( bindings.value( btn ) );
        if ( _mode == full_page )
            back_button->show();
        emit currentChanged();
    }
}

QWidget* FTTabWidget::currentWidget()
{
    QWidget* curr = panels->currentWidget();
    if ( curr && ( curr != index_tab ) )
        return curr;
    return NULL;
}

void FTTabWidget::setCurrentWidget(QWidget * cur )
{
    QAbstractButton* btn = NULL;
    if ( (btn = bindings.key( cur, NULL)) != NULL )
    {
        btn->setChecked( true );
        panels->setCurrentWidget( cur );
        if ( _mode == full_page )
            back_button->show();
    }
}

void FTTabWidget::showIndex()
{
    if ( _mode == full_page )
    {
        foreach ( QAbstractButton* btn, buttons )
            btn->setChecked( false );
        panels->setCurrentWidget( index_tab );
        back_button->hide();
    }
}

QLayout* FTTabWidget::createHorizontal()
{
    if ( _mode == full_page )
    {
        FTVBoxLayout* grid = new FTVBoxLayout();
        grid->addWidget( panels );
        grid->addWidget( back_button );
        FTVBoxLayout* index_layout = new FTVBoxLayout();
        int n = 0;
        FTHBoxLayout* bl = NULL;
        foreach ( QAbstractButton* btn, buttons )
        {
            if ( n == 0 )
            {
                bl = new FTHBoxLayout();
                bl->addWidget( btn );
                n++;
            }
            else
            {
                n = 0;
                bl->addWidget( btn );
                index_layout->addLayout( bl );
            }
        }
        if ( n == 1 )
            index_layout->addLayout( bl );
        index_layout->addStretch();
        foreach ( QWidget* w, extra_widgets )
            index_layout->addWidget( w );
        index_layout->addStretch();
        if ( index_tab->layout() )
            delete index_tab->layout();
        index_tab->setLayout( index_layout );
        return grid;
    }
    else
    {
        FTHBoxLayout* grid = new FTHBoxLayout();
        FTVBoxLayout* buttons_grid = new FTVBoxLayout();
        buttons_grid->addStretch();
        foreach ( QAbstractButton* btn, buttons )
            buttons_grid->addWidget( btn );
        buttons_grid->addStretch();
        foreach ( QWidget* w, extra_widgets )
            buttons_grid->addWidget( w );
        buttons_grid->addStretch();
        grid->addLayout( buttons_grid );
        grid->addWidget( panels, 2 );
        return grid;
    }
}

QLayout* FTTabWidget::createVertical()
{
    FTVBoxLayout* grid = new FTVBoxLayout();

    if ( _mode == full_page )
    {
        grid->addWidget( panels );
        grid->addWidget( back_button );
        FTVBoxLayout* index_layout = new FTVBoxLayout();
        foreach ( QAbstractButton* btn, buttons )
            index_layout->addWidget( btn );
        index_layout->addStretch();
        foreach ( QWidget* w, extra_widgets )
            index_layout->addWidget( w );
        if ( index_tab->layout() )
            delete index_tab->layout();
        index_tab->setLayout( index_layout );
    }
    else
    {
        FTHBoxLayout* buttons_grid = new FTHBoxLayout();
        foreach ( QAbstractButton* btn, buttons )
            buttons_grid->addWidget( btn );
        buttons_grid->addStretch();
        foreach ( QWidget* w, extra_widgets )
            buttons_grid->addWidget( w );
        grid->addLayout( buttons_grid );
        grid->addWidget( panels );
    }
    return grid;
}
